package net.zhengxinyun.performance.App.appCtrl;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.App.dto.report.FileDTO;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.api.entity.SysFile;
import net.zhengxinyun.performance.api.service.SysFileService;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.IncentiveEntity;
import net.zhengxinyun.performance.entity.IncentiveTypeEntity;
import net.zhengxinyun.performance.entity.IncentiveTypeItemEntity;
import net.zhengxinyun.performance.filter.IncentiveFilter;
import net.zhengxinyun.performance.filter.IncentiveTypeFilter;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.service.incentive.IncentiveService;
import net.zhengxinyun.performance.service.incentive.IncentiveTypeSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * App 正向激励接口
 */
@RestController
@RequestMapping("/app/inspire")
public class InsprireCtrl   extends  BaseCtrl {

    @Autowired
    private IncentiveService incentiveService;
    @Autowired
    private IncentiveTypeSerice incentTypeService;
    @Autowired
    private SysFileService sysFileService;

    @PostMapping("/items")
    public Result<HashMap<String,Object>> indexScore (@RequestBody IncentiveFilter dtofilter)
    {
        if(StringUtils.isBlank(dtofilter.getUserPhone()))
        {
            return ResultUtils.getResults(Result.Status.ERROR,"手机号为空",null);
        }
        UserFilter userInfo = getUserInfo(dtofilter.getUserPhone());

        //非超级管理员， 只能看自己的。
        if(!"admin".equals(userInfo.getUserPermissions()))
        {
            dtofilter.setDepartCode(userInfo.getDepartcode());
        }
        //设置类别过滤
        if(ObjectUtils.isNotBlank(dtofilter.getTypeId()))
        {
            dtofilter.setPointsType(dtofilter.getTypeId()+"");
        }
        List<Map<String, Object>> insprireItems = incentiveService.query(dtofilter);
        HashMap<String,Object> map = new HashMap<>();
        map.put("data",insprireItems);
        System.out.println("-------接口： /inspire/items");
        return  ResultUtils.getSuccessResults(map);
    }

    //获取所有正向激励
    @PostMapping("/getinsprires")
    public Result<HashMap<String, Object>> getinsrires(@RequestBody IncentiveFilter dtofilter)
    {
        if(StringUtils.isBlank(dtofilter.getUserPhone()))
        {
            return ResultUtils.getResults(Result.Status.ERROR,"手机号为空",null);
        }
        UserFilter userInfo = getUserInfo(dtofilter.getUserPhone());
        //非超级管理员， 只能看自己的。
        if(!"admin".equals(userInfo.getUserPermissions()))
        {
            dtofilter.setDepartCode(userInfo.getDepartcode());
        }
        List<IncentiveFilter> incentiveFilters = incentiveService.queryAll(dtofilter);
        for (IncentiveFilter item : incentiveFilters) {
            List<FileDTO> files  = new ArrayList<>();
            String fileId = item.getFileId();
            if(StringUtils.isNotBlank(fileId));
            {
                List<String> strings = Util.strToList(fileId);
                for (String ids : strings) {

                    SysFile sysFile = sysFileService.selectOne(Long.parseLong(ids));
                    FileDTO filedto =  new FileDTO();
                    if(ObjectUtils.isNotBlank(sysFile))
                    {
                        filedto.setId(sysFile.getId());
                        filedto.setUrl(sysFile.getUrl());
                    }

                    files.add(filedto);
                }
                item.setFilelists(files);
            }


        }

        HashMap<String,Object> map = new HashMap<>();
        map.put("data",incentiveFilters);
        System.out.println("/getinsprires");
        return ResultUtils.getSuccessResults(map);

    }
    //编辑正向激励
    @PostMapping("/editInsprire")
    public Result<String> editInsprire (@RequestBody IncentiveFilter filter)
    {
        String currentYear = getCurrentYear();
        String tel = filter.getUserPhone();
        UserFilter userInfo = getUserInfo(tel);
        String optionType = filter.getOptionType();
        IncentiveEntity entity = new IncentiveEntity();
        entity.setDepartCode(userInfo.getDepartcode());
        entity.setIncentive(filter.getIncentive());
        entity.setRecognitionUnit(filter.getRecognitionUnit());
        entity.setPoint(filter.getPoint());
        entity.setAudit("0");
        entity.setFileName(filter.getFileName());
        entity.setPointsType(filter.getPointsType());
        entity.setTypeItemId(filter.getTypeItemId());
        entity.setCommendedUnit(filter.getCommendedUnit());
        entity.setId(filter.getId());
        entity.setDateTime(filter.getDateTime());
        entity.setFileId(filter.getFileId());
        switch (optionType)
        {
            case "add":
                entity.setId(null);
                String insert = incentiveService.onlyinsert(entity);

                break;

            case "update":
                if(ObjectUtils.isBlank(entity.getId()))
                {
                    return  ResultUtils.getResults(Result.Status.ERROR," id不能为空");
                }
                String update = incentiveService.update(entity);
                break;
            case "del":
                if(ObjectUtils.isBlank(entity.getId()))
                {
                    return  ResultUtils.getResults(Result.Status.ERROR," id不能为空");
                }
                String delete = incentiveService.delete(entity);
                break;
        }
        System.out.println("/editInsprire");
        return ResultUtils.getSuccessResults("操作成功");
    }

    //事项类别定制
    @PostMapping("/getinspTypes")
    public Result<HashMap<String, Object>> getinspTypes(@RequestBody IncentiveTypeFilter filter)
    {
        IncentiveTypeEntity entity = new IncentiveTypeEntity();
        String currentYear = getCurrentYear();
        if(StringUtils.isBlank(filter.getDatetime()))
        {
            entity.setDatetime(currentYear);
        }
        List<IncentiveTypeFilter> incentiveTypeFilters = incentTypeService.queryType(entity);
        HashMap<String,Object>   map = new HashMap<>();
        map.put("data",incentiveTypeFilters);
        map.put("datetime",currentYear);
        System.out.println("/getinspTypes");
        return ResultUtils.getSuccessResults(map);

    }

    @PostMapping("/editType")
    public Result<String> editTypes(@RequestBody IncentiveTypeFilter filter)
    {
        String optionType = filter.getOptionType();
        IncentiveTypeEntity entity = new IncentiveTypeEntity();
        entity.setTypeName(filter.getTypeName());
        entity.setDatetime(filter.getDatetime());
        entity.setId(filter.getId());
        switch (optionType){
            case "add":
                entity.setId(null);
                int i = incentTypeService.insertType(entity);
                break;
            case "update":
                if(ObjectUtils.isBlank(entity.getId()))
                {
                    return ResultUtils.getResults(Result.Status.ERROR,"id不能为空");
                }
                incentTypeService.updateType(entity);
                break;
            case "del":
                if(ObjectUtils.isBlank(entity.getId()))
                {
                    return ResultUtils.getResults(Result.Status.ERROR,"id不能为空");
                }
                incentTypeService.deleteType(entity.getId());
                break;
        }
        System.out.println("/editType");
        return ResultUtils.getSuccessResults("操作成功");
    }

    //获取具体事项类别定制  小类
    @PostMapping("/getLittlerTypes")
    public Result<HashMap<String, Object>> getLittleType(@RequestBody IncentiveTypeItemEntity filter)
    {
        Long typeId = filter.getTypeId();
        List<IncentiveTypeItemEntity> itemLists = incentTypeService.queryItems(typeId);
        HashMap<String,Object>   map = new HashMap<>();
        map.put("data",itemLists);
        System.out.println("/getLittlerTypes");
        return ResultUtils.getSuccessResults(map);

    }
    //具体事项类型增删改  小类
    @PostMapping("/editLittlerTypes")
    public Result<String> editLittlerTypes(@RequestBody IncentiveTypeItemEntity filter)
    {
        Long typeId = filter.getTypeId();
        String optionType = filter.getOptionType();
        IncentiveTypeItemEntity entity = new IncentiveTypeItemEntity();
        entity.setTypeId(typeId);
        entity.setItemName(filter.getItemName());
        entity.setId(filter.getId());
        int opCode=0;
        switch (optionType){
            case "add":
                ArrayList<IncentiveTypeItemEntity> entitylists = new ArrayList<>();
                entity.setId(null);
                entitylists.add(entity);
                opCode = incentTypeService.insertItems(entitylists);
                break;
            case "update":
                if(ObjectUtils.isBlank(entity.getId()))
                {
                    return  ResultUtils.getResults(Result.Status.ERROR,"id不能为空",null);
                }
                opCode = incentTypeService.updateItems(entity);
                break;
            case "del":
                if(ObjectUtils.isBlank(entity.getId()))
                {
                    return  ResultUtils.getResults(Result.Status.ERROR,"id不能为空",null);
                }
                opCode = incentTypeService.deleteItems(entity.getId());
                break;
        }
        if(0==opCode)
        {
            System.out.println("/editLittlerTypes");
            return  ResultUtils.getResults(Result.Status.ERROR,"无数据更新",null);
        }
        System.out.println("/editLittlerTypes");
        return ResultUtils.getSuccessResults("操作成功");

    }




}
